<template>
	<view class="container">
	

		<uni-section title="安全信息" type="line">
			<view class="example">
				
				<!-- 展示不同的排列方式 -->
				<uni-forms >
					<uni-forms-item label="旧密码" required>
						<uni-easyinput  type="password"  v-model="basePwd.oldpwd" placeholder="请输入旧密码" />
					</uni-forms-item>
					<uni-forms-item label="新密码" required>
						<uni-easyinput type="password"   v-model="basePwd.pwd" placeholder="请输入新密码" />
					</uni-forms-item>
					<uni-forms-item label="确认密码" required>
						<uni-easyinput type="password"  v-model="basePwd.repwd" placeholder="请输入确认密码" />
					</uni-forms-item>
					<button type="primary" @click="editPwd()">修改</button>
				</uni-forms>
			</view>
		</uni-section>

		
		
		
		
	</view>
</template>
<script>
	import store from '@/store/index.js';//需要引入store
	import constant from '@/store/constant.js'
	const baseUrl=constant.baseUrl;
	const baseImageUrl=constant.baseImageUrl;
	
	
	import md5 from 'js-md5';
	export default {
		computed:{
			errorMessage(){
				return store.state.user.errorMessage
			}
			
		},
		onLoad(option) {
			
		var userInfo=JSON.parse(uni.getStorageSync("userInfo"))
		this.$data.oldPwd=userInfo.pwd;
		this.$data.userId=userInfo.userId;
		},
		data() {
			return {
				imageUrl:baseImageUrl,
				oldPwd:"",
				userId:0,
				
				basePwd:{
					oldpwd:'',
					pwd:'',
					repwd:''
				},
				// 表单数据
				alignmentFormData: {
					name: '',
					age: '',
				},
				// 单选数据源
				sexs: [{
					text: '男',
					value: 1
				}, {
					text: '女',
					value: 2
				}, {
					text: '保密',
					value: 0
				}],
				
				// 分段器数据
				current: 0,
				items: ['左对齐', '顶部对齐'],
				// 校验表单数据
				valiFormData: {
					name: '',
					age: '',
					introduction: '',
				},
			
				// 自定义表单数据
				customFormData: {
					name: '',
					age: '',
					hobby: []
				},
			
			
			}
		},
	
		
		onReady() {
			
		},
		methods: {
			selectPortrait(){
				let self=this;
				uni.chooseImage({
					crop:{"width":200,"height":200},
					count: 1,
					success: (res) => {
						
						uni.uploadFile({
							url: baseUrl+'/api/upload/uploadFile', 
							filePath: res.tempFilePaths[0],
							name: 'file',
							// formData: {
							// 	'user': 'test'
							// },
							success: (result) => {
								
								let data =JSON.parse(result.data);
								console.log(data)
								if(data.code==0){
									console.log(data.result.url)
									self.$data.baseData.portrait=data.result.url;
								}
								
							}
						});
					}
				})
			},
			onClickItem(e) {
				console.log(e);
				this.current = e.currentIndex
			},
			
			
			editPwd(){
				
				
				let self=this;
				if(this.$data.basePwd.oldpwd.length>0&&this.$data.basePwd.pwd.length>0&&this.$data.basePwd.repwd.length>0){
					
					      
					if(this.$data.basePwd.pwd==this.$data.basePwd.repwd){
						const md5Value = md5(this.$data.basePwd.oldpwd);
						if(md5Value==this.$data.oldPwd){
							let data={
								"userId":this.$data.userId,
								"pwd":this.$data.basePwd.pwd,
								"oldPwd":this.$data.basePwd.oldpwd,
								
							}
							store.dispatch('editPwdData',data).then(()=>{
								var newPwd=md5(self.$data.basePwd.pwd);
								var userInfo=JSON.parse(uni.getStorageSync("userInfo"));
								userInfo.pwd=newPwd;
								self.$data.oldPwd=newPwd;
								uni.setStorageSync('userInfo', JSON.stringify(userInfo));
								uni.showToast({
									icon: 'none',
									title: "密码修改成功" 
								})
								setTimeout(() => {
									
									uni.switchTab({
										url:"/pages/user/userInfoAd"
									})
								}, 1000);
								
								
							});
						}else{
							uni.showToast({
								icon: 'none',
								title: "旧密码不正确" 
							})
						}
						
					}else{
						uni.showToast({
							icon: 'none',
							title: "新密码和确认密码不一致" 
						})
					}
				}else{
					uni.showToast({
						icon: 'none',
						title: "相关的密码字段不可以为空" 
					})
				}
				
			},
			logout(){
				uni.removeStorageSync('authInfo');
				uni.navigateTo({
					url:"/pages/login/login"
				})
			}
		}
	}
</script>
<style lang="scss">

	.example {
		padding: 15px;
		background-color: #fff;
	}

	.segmented-control {
		margin-bottom: 15px;
	}

	.button-group {
		margin-top: 15px;
		display: flex;
		justify-content: space-around;
	}

	.form-item {
		display: flex;
		align-items: center;
	}

	.button {
		display: flex;
		align-items: center;
		height: 35px;
		margin-left: 10px;
	}
</style>